package io.renren.modules.project.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.MapUtils;
import io.renren.modules.project.dao.ProjectExpachDao;
import io.renren.modules.project.entity.ProjectExpachEntity;
import io.renren.modules.project.service.ProjectExpachService;
import io.renren.modules.project.vo.ExpachVoEntity;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;



@Service("projectExpachService")
public class ProjectExpachServiceImpl extends ServiceImpl<ProjectExpachDao, ProjectExpachEntity> implements ProjectExpachService {

    @Override
    public List<ExpachVoEntity> queryVoList(Map<String, Object> params) {
        String projectNo = (String)params.get("projectNo");

        List<ProjectExpachEntity> list = this.selectList(
                new EntityWrapper<ProjectExpachEntity>().eq("project_no",projectNo)
                        .orderBy("bill_code",true).orderBy("seq_num",true)
        );

        List<ExpachVoEntity> voList = new ArrayList<>();
        Long billCode = 0L;

        for(ProjectExpachEntity entity : list) {
            if(entity.getBillCode() != billCode) {
                ExpachVoEntity voEntity = new ExpachVoEntity();
                voEntity.setAchList(new ArrayList<>());
                voList.add(voEntity);
                billCode = entity.getBillCode();
                voEntity.setBillCode(billCode);
                voEntity.setCreateUsername(entity.getCreateUsername());
                voEntity.setCreateTime(entity.getCreateTime());
            }
            voList.get(voList.size() - 1).getAchList().add(entity);
        }

        return voList;
    }

    @Override
    public List<ProjectExpachEntity> queryList(Map<String, Object> params){
        String projectNo = params.get("projectNo").toString();
        String billCode = params.get("billCode").toString();
        List<ProjectExpachEntity> list = this.selectList(
                new EntityWrapper<ProjectExpachEntity>().eq("project_no",projectNo).eq("bill_code",billCode)
                        .orderBy("bill_code",true).orderBy("seq_num",true)
        );
        return list;
    }

    @Override
    public List<ProjectExpachEntity> queryLastList(Map<String, Object> params){
        String projectNo = params.get("projectNo").toString();
        Long billCode = this.baseMapper.getMaxBillcode(params);
        List<ProjectExpachEntity> list = new ArrayList<>();
        if (billCode != null) {
            list = this.selectList(
                    new EntityWrapper<ProjectExpachEntity>().eq("project_no",projectNo).eq("bill_code",billCode)
                            .orderBy("bill_code",true).orderBy("seq_num",true)
            );
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(ExpachVoEntity voEntity) {
        SysUserEntity userEntity = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal());
        if (voEntity.getBillCode() == null || voEntity.getBillCode() == 0) {
            Map<String,Object> params = new MapUtils().put("projectNo",voEntity.getProjectNo());
            Long billCode = this.getMaxBillcode(params);
            voEntity.setBillCode(billCode);
        }
        for (ProjectExpachEntity entity : voEntity.getAchList()) {
            entity.setBillCode(voEntity.getBillCode());
            entity.setCreateTime(new Date());
            entity.setCreateUsername(userEntity.getUsername());
            entity.setProjectNo(voEntity.getProjectNo());
        }
        // 保存时线先删除 已有数据
        this.delete(new EntityWrapper<ProjectExpachEntity>().eq("project_no",voEntity.getProjectNo()).eq("bill_code",voEntity.getBillCode()));

        this.insertBatch(voEntity.getAchList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ProjectExpachEntity entity) {
        this.updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(ExpachVoEntity voExpach) {
        //删除
        this.delete(new EntityWrapper<ProjectExpachEntity>()
                .eq("project_no",voExpach.getProjectNo())
                .eq("bill_code",voExpach.getBillCode()));

    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long getMaxBillcode(Map<String, Object> params){
        Long maxCode = this.baseMapper.getMaxBillcode(params);
        return maxCode == null ? 1 : (maxCode + 1);
    }
}